package com.xzk.ExpressInnWeb.dao.daoImp;

import com.xzk.ExpressInnWeb.dao.BaseAdminDao;
import com.xzk.ExpressInnWeb.util.DruidUtil;
import com.xzk.ExpressInnWeb.util.ParamType;
import com.xzk.ExpressInnWeb.util.QueryUtil;

import java.sql.*;
import java.util.Date;

public class AdminDaoMySQL implements BaseAdminDao {

    // 登录时间也可以直接使用MySQL的now()函数调取当前DateTime
    // 即 ... logintime=now() ...
    private static final String SQL_UPDATE_LOGIN_TIME = "update admin set logintime=?, loginip=? where username=?";
    // 查询 * 可能导致数据库性能损耗
    private static final String SQL_LOGIN = "select id from admin where username=? and password=?";
    // 根据账户名查询电话号码
    private static final String SQL_ADMIN_PHONE = "SELECT ADMINPHONE FROM ADMIN WHERE USERNAME=?";

    private Connection conn = null;
    private PreparedStatement state = null;
    private ResultSet rs = null;

    @Override
    public void updateLoginTime(String username, Date date, String IP) {
        // 1. 获取连接
        Connection connection = DruidUtil.getConnection();
        // 2. 预编译 SQL 语句
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            // 3. 填充参数
            // 注意 util 包的 date 需要转换为 sql 包的 Timestamp
            preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
            preparedStatement.setString(2, IP);
            preparedStatement.setString(3, username);
            // 4. 执行SQL语句
            preparedStatement.executeUpdate();
            // 当前无需获取返回值
            // 真实项目中可能需要记录日志等
        } catch (SQLException|NullPointerException throwables) {
            throwables.printStackTrace();
        } finally {
            // 5. 释放资源
            DruidUtil.close(connection, preparedStatement);
        }
    }

    @Override
    public boolean login(String username, String password) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(SQL_LOGIN);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            // 根据查询结果返回true或false
            // 因id唯一(Primary Key)，故只有两种结果
            // 要么查询到唯一的id，要么什么都查询不到
            // 若resultSet的游标能够向下移动，则证明查询到数据，
            // 故返回next()即可
            return resultSet.next();
        } catch (SQLException|NullPointerException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        // 若出现异常，仍然给false的结果，即登录失败
        return false;
    }

    @Override
    public String getAdminPhone(String username) {
        String adminphone = "";
        rs = QueryUtil.query(
                SQL_ADMIN_PHONE,
                QueryUtil.getList(
                        QueryUtil.getMap(ParamType.STRING, username)),
                state,conn);
        try {
            if (rs.next()){
                adminphone = rs.getString("adminphone");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn,state,rs);
        }
        return adminphone;
    }
}
